﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.Outstorage
{
    public partial class ListOutStorageRecord : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                    if (roles_id == 15)//仓储班长
                    {
                        lcs.BLL.storeinfo bst = new BLL.storeinfo();
                        ddlstore.DataSource = bst.GetddlList("");
                        ddlstore.DataTextField = "sname";
                        ddlstore.DataValueField = "sid";
                        ddlstore.DataBind();
                        ddlstore.Items.Insert(0, new ListItem("所有", "-1"));

                    }
                    else if (roles_id == 16)//领导
                    {
                        lcs.BLL.storeinfo bst = new BLL.storeinfo();
                        ddlstore.DataSource = bst.GetddlList("");
                        ddlstore.DataTextField = "sname";
                        ddlstore.DataValueField = "sid";
                        ddlstore.DataBind();
                        ddlstore.Items.Insert(0, new ListItem("所有", "-1"));
                    }
                    else
                    {
                        lcs.BLL.storeinfo bst = new BLL.storeinfo();
                        ddlstore.DataSource = bst.GetddlList(" uid=" + getid());
                        ddlstore.DataTextField = "sname";
                        ddlstore.DataValueField = "sid";
                        ddlstore.DataBind();
                        ddlstore.Items.Insert(0, new ListItem("所有", "-1"));

                    }


                    BindGV(user_id, roles_id);

                    //if (roles_id == 2)//仓管员
                    //{

                    //}
                    //else
                    //{
                    //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您不是仓库管理员！');</script>");
                    //}

                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
                }
            }


        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                BindGV(user_id, roles_id);
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
            }

        }

        private void BindGV(int out_storage_User_id, int roles_id)
        {
            string startTime = this.txt_startTime.Text;
            string endTime = this.txt_endTime.Text;

            string sql = " 1=1 ";

            if (roles_id == 15 )//如果是仓储班长查询
            {
                
            }
            else if (roles_id == 16)//如果是领导查询
            {
                
            }
            else
            {
                sql += " and out_storage_User_id=" + out_storage_User_id + " and storeUid= " + out_storage_User_id;
            }

            if (!string.IsNullOrEmpty(this.txt_proid.Text))
            {
                sql += " and proid  like'%" + this.txt_proid.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_projectname.Text))
            {
                sql += " and projectname like '%" + this.txt_projectname.Text + "%' ";
            }

            if (!string.IsNullOrEmpty(this.txt_material.Text))
            {
                sql += " and material like '%" + this.txt_material.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_receive_address.Text))
            {
                sql += " and receive_address like '%" + this.txt_receive_address.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_targetProjectName.Text))
            {
                sql += " and targetProjectName like '%" + this.txt_targetProjectName.Text + "%' ";
            }
            if (ddlstore.SelectedValue != "-1")
            {
                sql += " and sid = '" + ddlstore.SelectedValue + "' ";
            }
            if (!string.IsNullOrEmpty(startTime))
            {
                sql += " and out_storage_Time >= '" + startTime + "' ";
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                sql += " and out_storage_Time <= '" + endTime + "' ";
            }

            sql += " and mid is not null ";

            //DataSet ds = new lcs.BLL.out_storage().GetOutList(sql);

            lcs.BLL.out_storage om = new lcs.BLL.out_storage();

            DataSet ds = om.GetListByPageOutList(sql, "",
                (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + 1,
                this.AspNetPager1.CurrentPageIndex * this.AspNetPager1.PageSize);

            this.AspNetPager1.RecordCount = om.GetRecordCountOutList(sql);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

            //this.lbl_count.Text = ds.Tables[0].Rows.Count.ToString();

            this.lbl_count.Text = ds.Tables[0].Rows.Count.ToString();

            decimal price = 0;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                decimal count = decimal.Parse(row["out_storage_count"].ToString());
                price += (decimal.Parse(row["unitprice"].ToString()) * count);
            }

            this.lbl_price.Text = price.ToString("f2");

            DataSet ds_all = om.GetOutList(sql);

            decimal priceall = 0;

            foreach (DataRow row in ds_all.Tables[0].Rows)
            {
                decimal count = decimal.Parse(row["out_storage_count"].ToString());
                priceall += (decimal.Parse(row["unitprice"].ToString()) * count);
            }

            this.lbl_priceall.Text = priceall.ToString("f2");

        }
        //查询
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());


                BindGV(user_id, roles_id);
            }

        }
        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                BindGVNoPage(user_id, roles_id);
            }

            this.GridView1.Columns[0].Visible = false;
            this.GridView1.Columns[2].Visible = false;

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=chukujilu.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>出库记录</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                        e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }

                Label lbl_count = e.Row.FindControl("lbl_count") as Label;
                Label lbl_unit = e.Row.FindControl("lbl_unit") as Label;
                Label lbl_xiaoji = e.Row.FindControl("lbl_xiaoji") as Label;

                decimal unitd = Convert.ToDecimal(lbl_unit.Text);
                decimal countd = Convert.ToDecimal(lbl_count.Text);

                decimal xjd = unitd * countd;

                xjd = Math.Round(xjd, 2);

                lbl_xiaoji.Text = xjd.ToString();

            }
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                BindGV(user_id, roles_id);
            }
        }



        private void BindGVNoPage(int out_storage_User_id, int roles_id)
        {
            string startTime = this.txt_startTime.Text;
            string endTime = this.txt_endTime.Text;

            string sql = " 1=1 ";

            if (roles_id != 15)//如果不是仓储班长查询
            {
                sql += " and out_storage_User_id=" + out_storage_User_id + " and storeUid= " + out_storage_User_id;
            }


            if (!string.IsNullOrEmpty(this.txt_proid.Text))
            {
                sql += " and proid  like'%" + this.txt_proid.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_projectname.Text))
            {
                sql += " and projectname like '%" + this.txt_projectname.Text + "%' ";
            }

            if (!string.IsNullOrEmpty(this.txt_material.Text))
            {
                sql += " and material like '%" + this.txt_material.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_receive_address.Text))
            {
                sql += " and receive_address like '%" + this.txt_receive_address.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_targetProjectName.Text))
            {
                sql += " and targetProjectName like '%" + this.txt_targetProjectName.Text + "%' ";
            }
            if (ddlstore.SelectedValue != "-1")
            {
                sql += " and sid = '" + ddlstore.SelectedValue + "' ";
            }
            if (!string.IsNullOrEmpty(startTime))
            {
                sql += " and out_storage_Time >= '" + startTime + "' ";
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                sql += " and out_storage_Time <= '" + endTime + "' ";
            }

            sql += " and mid is not null ";

            lcs.BLL.out_storage om = new lcs.BLL.out_storage();

            DataSet ds = om.GetOutList(sql);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

            //this.lbl_count.Text = ds.Tables[0].Rows.Count.ToString();

            //decimal price = 0;

            //foreach (DataRow row in ds.Tables[0].Rows)
            //{
            //    decimal count = decimal.Parse(row["out_storage_count"].ToString());
            //    price += (decimal.Parse(row["unitprice"].ToString()) * count);
            //}

            //this.lbl_price.Text = price.ToString("f2");
        }


    }
}